
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Jason Date  2012/05/11 19:22
 */
public class ClassProvider {

    public static void main(String[] args) throws IOException {
        String entityPath = "/home/user192/lotary/projects/customs/customs-dmo/src/java/com/cn/lotary/entity";
        String controllerpath = "/home/user192/lotary/projects/oa//OA-war/src/java/com/cn/lotary/web/controller";
        String daopath = "/home/user192/NetBeansProjects/test-del/src/java/com/cn/lotary/dao";

       // providerClass(entityPath, daopath, ClasType.Dao);
        //providerClass(entityPath, controllerpath, ClasType.Controller);
         remvoeNotNull(entityPath);
    }

    private static void providerClass(String entityPath, String daoPath, ClasType extension) throws IOException {
        File file = new File(entityPath);
        if (!file.exists()) {
            System.out.println("the path [" + entityPath + "] deso not exists !");
            return;
        }
        for (File f : file.listFiles()) {
            String entityName = f.getName();
            File l = new File(daoPath);
            if (!l.exists()) {
                l.mkdirs();
            }
            String fileanme = entityName.substring(0, entityName.indexOf("."));

            String newfullpath = daoPath + "/" + fileanme + extension.name() + ".java";

            File newfile = new File(newfullpath);
            if (!newfile.exists()) {
                newfile.createNewFile();
            }

            FileWriter fileWriter = new FileWriter(newfile, true);

            fileWriter.write(extension.equals(ClasType.Dao) ? daoClassModel(fileanme) : controllerClassModel(fileanme));
            fileWriter.close();
        }
    }

    private static String daoClassModel(String entityname) {
        String outcome =
                "package com.cn.lotary.dao;\r\n"
                + "\r\n"
                + "import com.cn.lotary.base.dao.BaseDao;\r\n"
                + "import com.cn.lotary.entity." + entityname + ";\r\n"
                + "import javax.ejb.Stateless;\r\n"
                + "import javax.persistence.EntityManager;\r\n"
                + "import javax.persistence.PersistenceContext;\r\n"
                + "\r\n"
                + "/**\r\n"
                + " *\r\n"
                + " * @author LotaryBase ClassProvider\r\n"
                + " */\r\n"
                + "@Stateless\r\n"
                + "public class " + entityname + "Dao extends BaseDao<" + entityname + "> {\r\n"
                + "\r\n"
                + "    @PersistenceContext\r\n"
                + "    private EntityManager em;\r\n"
                + "\r\n"
                + "    public " + entityname + "Dao() {\r\n"
                + "        super(" + entityname + ".class);\r\n"
                + "    }\r\n"
                + "\r\n"
                + "    @Override\r\n"
                + "    public EntityManager getEntityManager() {\r\n"
                + "        return em;\r\n"
                + "    }\r\n"
                + "}\r\n";

        return outcome;
    }

    private static String controllerClassModel(String entityname) {
        String outcome =
                "package com.cn.lotary.web.controller;\r\n"
                + "\r\n"
                + "import com.cn.lotary.base.controller.AbstractController;\r\n"
                + "import com.cn.lotary.base.dao.BaseDao;\r\n"
                + "import com.cn.lotary.dao." + entityname + "Dao;\r\n"
                + "import com.cn.lotary.entity." + entityname + ";\r\n"
                + "import javax.ejb.EJB;\r\n"
                + "import javax.faces.bean.ManagedBean;\r\n"
                + "import javax.faces.bean.ViewScoped;\r\n"
                + "\r\n"
                + "/**\r\n"
                + " *\r\n"
                + " * @author user192\r\n"
                + " */\r\n"
                + "@ManagedBean\r\n"
                + "@ViewScoped\r\n"
                + "public class " + entityname + "Controller extends AbstractController<" + entityname + "> {\r\n"
                + "\r\n"
                + "    @EJB\r\n"
                + "    private " + entityname + "Dao dao;\r\n"
                + "\r\n"
                + "    @Override\r\n"
                + "    protected BaseDao<" + entityname + "> dao() {\r\n"
                + "        return dao;\r\n"
                + "    }\r\n"
                + "\r\n"
                + "    @Override\r\n"
                + "    protected String selectItemLabel() {\r\n"
                + "          //TODO:check return value\r\n"
                + "          return \"getName\";\r\n"
                + "    }\r\n"
                + "}\r\n";
        return outcome;
    }

    /**
     * remove id column @NotNull
     *
     * @param entityPath
     */
    private static void remvoeNotNull(String entityPath) {
        File files = new File(entityPath);
        if (files.isDirectory()) {
            for (File entity : files.listFiles()) {
                String name = entity.getName();
                if (name.endsWith(".java")) {
                    try {
                        //TODO: remove id @notNull
                        Scanner scanner = new Scanner(entity);
                        String newStr = "";
                        while (scanner.hasNext()) {
                            newStr = newStr + scanner.nextLine() + "\r\n";
                        }

                        newStr = newStr.replace("@Id\r\n    @Basic(optional = false) \r\n    @NotNull", "@Id\r\n    @Basic(optional = false) \r\n");
                        entity.delete();
                        FileWriter writer = new FileWriter(entity);
                        writer.write(newStr);
                        writer.close();
                        scanner.close();
                    } catch (Exception ex) {
                        System.err.println("remvoeNotNull:  " + ex.getLocalizedMessage());
                    }
                }
            }
        }

    }
}

enum ClasType {

    Controller,
    Dao;
}
